

const {ccclass, property} = cc._decorator;

@ccclass
export default class FPJ_ListViewCtrl extends cc.Component {

    @property(cc.Prefab)
    itemTemplate: cc.Prefab = null;

    @property(cc.ScrollView)
    scrollView:cc.ScrollView = null;

    @property(cc.Integer)
    spawnCount = 7;

    @property(cc.Float)
    spacing = 10;

    @property(cc.Float)
    bufferZone = 400;

    content = null;
    items = [];

    totalCount = 0;
    updateTimer = 0;
    lastContentPosY = 0;
    updateInterval = 0;

    _data  = null;

    _friendListCom = null;
    start () {

    }


    onEnable(){

    }

    initData(data,friendListCom = null){
        this._data = data;
        this._friendListCom = friendListCom;
        this.content = this.scrollView.content;
        this.items = [];
        this.initialize( this._data )

        this.updateTimer = 0;
        this.updateInterval = 0.2;
        this.lastContentPosY = 0;
    }

    initialize(list) {

        for(let i in this.content.children){
            this.content.children[i].active = false;
        }

        this.totalCount = list.length;
        if(this.totalCount <= 0){
            return ;
        }
        this.content.height = this.totalCount * (this.itemTemplate.data.height + this.spacing) + this.spacing;
        for (let i = 0; i < this.spawnCount && i < this._data.length ; ++i) {

            let item = null;
            if(this.content.children[i]){
                item = this.content.children[i];
            }else {
                item = cc.instantiate(this.itemTemplate);
                this.content.addChild(item);
            }
            item.active = true;

            item.setPosition(0, -item.height * (0.5 + i) - this.spacing * (i + 1));
            //item.setPosition(0, -item.height * (0 + i) - this.spacing * (i + 1));

            //设置item数据
            item.getComponent(this.itemTemplate.data.name).init(i,this._data[i],this._friendListCom);
            this.items.push(item);
        }
    }
    getPositionInView (item) {
        let worldPos = item.parent.convertToWorldSpaceAR(item.position);
        let viewPos = this.scrollView.node.convertToNodeSpaceAR(worldPos);
        return viewPos;
    }
    update(dt) {
        this.updateTimer += dt;
        if (this.updateTimer < this.updateInterval) return;
        this.updateTimer = 0;
        let items = this.items;
        let buffer = this.bufferZone;
        let isDown = this.scrollView.content.y < this.lastContentPosY;
        let offset = (this.itemTemplate.data.height + this.spacing) * items.length;
        for (let i = 0; i < items.length; ++i) {
            let viewPos = this.getPositionInView(items[i]);
            if (isDown) {
                if (viewPos.y < -buffer && items[i].y + offset < 0) {
                    items[i].y = items[i].y + offset;

                    //设置item数据
                    let item = items[i].getComponent(this.itemTemplate.data.name);
                    let itemId = item.itemID - items.length;
                    item.init(itemId,this._data[itemId] ,this._friendListCom);
                }
            } else {
                // if away from buffer zone and not reaching bottom of content
                if (viewPos.y > buffer && items[i].y - offset > -this.content.height) {
                    items[i].y = items[i].y - offset;

                    //设置item数据
                    let item = items[i].getComponent(this.itemTemplate.data.name);
                    let itemId = item.itemID + items.length;
                    item.init( itemId,this._data[itemId] ,this._friendListCom);
                }
            }
        }
        this.lastContentPosY = this.scrollView.content.y;
    }

}
